<?php
// +----------------------------------------------------------------------
// | Copyright © Sogu Technologies Co. Ltd 2018-2050 All rights reserved.
// +----------------------------------------------------------------------
// | File name: RepaySmsRemind.php
// +----------------------------------------------------------------------
// | Author: gxs
// +----------------------------------------------------------------------
// | Description: 还款短信发送trait
// +----------------------------------------------------------------------
// | Others: 其他内容的说明
// +----------------------------------------------------------------------
// | History: 修改历史记录列表，每条修改记录应包含修改日期、修改者及修改内容简述
// +----------------------------------------------------------------------
// | 1.Date: 2019/1/16 Author: gxs Modification: 创建
// +----------------------------------------------------------------------

namespace App\Library\Traits;

use App\Constant\SmsTemplate;
use App\Models\RepayRecord;
use App\Models\RepayRecordMoney;
use App\Models\RepaySummary as RepaySummaryModel;
use App\Models\User;
use App\Models\UserBankCard;
use App\Models\UserCertOcr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;

trait RepaySmsRemind
{
    /**
     * Description: 发送提前结清成功短信
     * Author: Gxs
     * @param $summary_model
     * @param $model
     * @return bool
     */
    public static function sendSettlementSuccessSms($summary_model, $model)
    {
        //短信推送 用户信息获取
        $phone_num = $model->relationUsers->phone_num;
        $name = self::_getSmsUserName($model);
        Log::debug('send_settlement_success', ['phone_num' => $phone_num, 'name' => $name, 'amount' => $summary_model->amount, 'term' => $summary_model->period_count]);
        //全部结清成功发送短信
        SmsTemplate::send(SmsTemplate::TEMPLATE_EARLY_SETTLEMENT_SUCCESS, $phone_num, [
            'name' => $name,  //用户姓名
            'amount' => $summary_model->amount, //金额
            'term' => $summary_model->period_count, //期数
        ]);
        return true;
    }


    /**
     * Description: 发送提前结清失败短信
     * Author: Gxs
     * @param $model
     * @return bool
     */
    public static function sendSettlementFailureSms($model)
    {
        //短信推送 用户信息获取
        $phone_num = $model->relationUsers->phone_num;
        $name = self::_getSmsUserName($model);
        Log::debug('send_settlement_failure', ['name' => $name]);
        //全部结清成功发送短信
        SmsTemplate::send(SmsTemplate::TEMPLATE_EARLY_SETTLEMENT_FAIL, $phone_num, [
            'name' => $name,  //用户姓名
        ]);
        return true;
    }

    /**
     * Description: 手动还款成功
     * Author: Gxs
     * @param RepayRecordMoney $model
     */
    public static function sendHandleSuccessSms($model)
    {
        //短信推送 用户信息获取
        $phone_num = $model->relationUsers->phone_num;
        $name = self::_getSmsUserName($model);
        $total_amount = self::_getSmsTotalAmount($model);
        Log::debug('send_handle_success', ['phone_num' => $phone_num, 'name' => $name, 'eachAmount' => $total_amount, 'term' => $model->period]);
        SmsTemplate::send(SmsTemplate::TEMPLATE_HANDLE_REPLAY_SUCCESS, $phone_num, [
            'name' => $name,  //用户姓名
            'eachAmount' => $total_amount, //本期应还金额
            'term' => $model->period, //当前期数
        ]);
    }

    /**
     * Description: 手动还款失败
     * Author: Gxs
     * @param $model
     */
    public static function sendHandleFailureSms($model)
    {
        //短信推送 用户信息获取
        $phone_num = $model->relationUsers->phone_num;
        $name = self::_getSmsUserName($model);
        Log::debug('send_handle_failure', ['name' => $name]);
        //手动还款成功
        SmsTemplate::send(SmsTemplate::TEMPLATE_HANDLE_REPLY_FAIL, $phone_num, [
            'name' => $name,  //用户姓名
        ]);
    }

    /**
     * Description:逾期发送短信
     * Author:lijiafei
     */
    public static function sendOverdueSms(){
        $before_three_days = Carbon::parse('-3 days')->toDateString();
        $before_one_days = Carbon::parse('-1 days')->toDateString();
        //逾期三天
        foreach (RepayRecord::with(['users'])->where('status','<>',RepayRecord::STATUS_FOUR)->where('pay_off','=',RepayRecord::PAY_OFF_ONE)->where('normal_date','=',$before_three_days)->get() as $record){

            $real_name = isset($record->users->real_name) ? $record->users->real_name : '';
            $phone_num = isset($record->users->phone_num) ? $record->users->phone_num : 0;
            $create_at = isset($record->created_at) ? $record->created_at : '';
            if($phone_num && $create_at){
                $month = $create_at->month;
                $day = $create_at->day;
                $sendData['name'] = $real_name;  //用户姓名
                $sendData['month'] = $month; //月份
                $sendData['day'] = $day; //天
                SmsTemplate::send(SmsTemplate::TEMPLATE_REPLY_OVERDUE_THREE_DAY,$phone_num,$sendData);
            }
        }

        //逾期一天
        foreach (RepayRecord::with(['users'])->where('status','<>',RepayRecord::STATUS_FOUR)->where('pay_off','=',RepayRecord::PAY_OFF_ONE)->where('normal_date','=',$before_one_days)->get() as $record){

            $real_name = isset($record->users->real_name) ? $record->users->real_name : '';
            $phone_num = isset($record->users->phone_num) ? $record->users->phone_num : 0;
            $create_at = isset($record->created_at) ? $record->created_at : '';
            if($phone_num && $create_at){
                $month = $create_at->month;
                $day = $create_at->day;
                $sendData['name'] = $real_name;  //用户姓名
                $sendData['month'] = $month; //月份
                $sendData['day'] = $day; //天
                SmsTemplate::send(SmsTemplate::TEMPLATE_REPLY_OVERDUE_ONE_DAY,$phone_num,$sendData);
            }
        }
    }

    /**
     * Description: 自动还款成功短信
     * Author: Gxs
     * @param RepayRecordMoney $model
     */
    public static function sendAotuSuccessSms($model)
    {
        $phone_num = $model->relationUsers->phone_num;
        $name = self::_getSmsUserName($model);
        $total_amount = self::_getSmsTotalAmount($model);

        SmsTemplate::send(SmsTemplate::TEMPLATE_AUTOMATIC_REPLY_SUCCESS, $phone_num, [
            'name' => $name,  //用户姓名
            'eachAmount' => $total_amount, //本期应还金额
            'term' => $model->period, //当前期数
        ]);
    }

    /**
     * Description: 自动还款失败短信
     * Author: Gxs
     * @param $model
     */
    public static function sendAotuFailureSms($model)
    {
        $phone_num = $model->relationUsers->phone_num;
        $name = self::_getSmsUserName($model);
        $card_num = UserBankCard::getCardInfoByBankCardId($model->bank_cards_id);
        $card_num = empty($card_num) ? '****' : $card_num['card_num'];
        SmsTemplate::send(SmsTemplate::TEMPLATE_AUTOMATIC_REPLY_FAIL, $phone_num, [
            'name' => $name,  //用户姓名
            'cardNo' => $card_num, //还款银行卡尾号
        ]);
    }

    /**
     * Description: 获取用户名
     * Author: Gxs
     * @param $model
     * @return string
     */
    private static function _getSmsUserName($model)
    {
        $name = UserCertOcr::where('user_id', $model->uid)
            ->where('is_valid', UserCertOcr::IS_VALID)
            ->value('name');
        if (empty($name))
            $name = '用户';
        return $name;
    }

    /**
     * Description: 获取本期应还金额
     * Author: Gxs
     * @param RepayRecordMoney $model
     * @return string
     */
    private static function _getSmsTotalAmount($model)
    {
        //手动还款成功
        $amount = bcadd($model->record->amount, $model->month->amount, 2); //本期本金 && 本期月保费
        //公共平台相关
        $common_one = bcadd($model->common->lending_money, $model->common->fines_money, 2);
        $common_total = bcadd($common_one, $model->common->manage_money, 2);
        //资金平台相关
        $money_one = bcadd($model->lending_money, $model->fines_money, 2);
        $money_two = bcadd($model->manage_money, $model->repayment_money, 2);
        $money_total = bcadd($money_one, $money_two, 2);
        $total_amount = bcadd(bcadd($common_total, $money_total, 2), $amount, 2);
        return $total_amount;
    }
}